package com.qf.controller;

import com.qf.commons.SseEmitterUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@CrossOrigin("*")
@Controller
public class EventSourceConnectionController  {

    // 该方法的作用是让前端EventSource来连接的，方法的返回值要 SseEmiter
    @GetMapping(value = "/se/conn/{userId}")
    public SseEmitter connection(@PathVariable("userId") String userId) {
        SseEmitter sseEmitter = new SseEmitter();
        SseEmitterUtils.seMap.put(userId, sseEmitter);
        return sseEmitter;
    }
}
